package org.joyflow.core.controller;

import lombok.RequiredArgsConstructor;
import org.joyflow.core.common.R;
import org.joyflow.core.entity.MetaTable;
import org.joyflow.core.entity.MetaTableDir;
import org.joyflow.core.entity.MetaTableField;
import org.joyflow.core.service.MetaTableServiceInterface;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/meta-table")
@RequiredArgsConstructor
public class MetaTableController {
    private final MetaTableServiceInterface metaTableService;


    @RequestMapping("/dirs")
    public R dirs() {
        List<MetaTableDir> dirs = metaTableService.getDirs();
        return R.success(dirs);
    }

    @RequestMapping("/tables")
    public R tables() {
        List<MetaTable> tables = metaTableService.getTables();
        return R.success(tables);
    }

    @RequestMapping("/tablesByDirId")
    public R getTablesByDirId(Long dirId) {
        List<MetaTable> tables = metaTableService.getTablesByDirId(dirId);
        return R.success(tables);
    }

    @RequestMapping("/fieldsByTableId")
    public R getFieldsByTableId(Long metaTableId) {
        List<MetaTableField> fields = metaTableService.getFieldsByMetaTableId(metaTableId);
        return R.success(fields);
    }

    @RequestMapping("/generateDDL")
    public R generateDDL( Long metaTableId) {
        return R.success(metaTableService.generateDDL(metaTableId));
    }

    @RequestMapping("/preview")
    public R preview(Long metaTableId, int pageIndex, int pageSize) {
        return R.success(metaTableService.preview(metaTableId, pageIndex, pageSize));
    }



}
